<?php
/**
 * OWL Error class
 *
 * @package OWL
 * @subpackage Core
 * @author trung
 * @since 1.0
 */

/* Error Code 
 *      Code    Cause                                   ErrorData
 *      5xx     Related Model Class
 *      500     Happen in owl-db
 *      501     Happen in owl-option
 *      502     Happen in owl-course
 *      503     Happen in owl-note
 *      504     Happen in owl-product
 *      505     Hapeen in owl-personal
 *      506     Happen in owl-page
 *      507     Happen in owl-ulang
 */

class Owl_Error {
    
    var $errors = array();
    var $error_data = array();

    function  __construct($code = '', $message = '', $data = '') {
        $this->Owl_Error($code, $message, $data);
    }
    
    function Owl_Error($code = '', $message = '', $data = '') {
		if ( empty($code) )
                    return;
                    
		$this->errors[$code] = $message;

		if ( ! empty($data) )
			$this->error_data[$code] = $data;
    }

    function get_error_codes() {
		if ( empty($this->errors) )
			return array();

		return array_keys($this->errors);
    }
    
    function get_error_messages($code = '') {
		// Return all messages if no code specified.
		if ( empty($code) ) {
			$all_messages = array();
			foreach ( (array) $this->errors as $code => $messages )
				$all_messages = array_merge($all_messages, $messages);
			return $all_messages;
		}
		if ( isset($this->errors[$code]) )
			return $this->errors[$code];
		else
			return array();
    }

    function get_error_data($code = '') {
		if ( empty($code) )
			$code = $this->get_error_code();

		if ( isset($this->error_data[$code]) )
			return $this->error_data[$code];
		return null;
    }
    
    function add($code, $message, $data = '') {
		$this->errors[$code] = $message;
		if ( ! empty($data) )
			$this->error_data[$code] = $data;
    }

    function add_data($data, $code = '') {
		if ( empty($code) )
			$code = $this->get_error_code();

		$this->error_data[$code] = $data;
    }

    function print_error(){
       echo Owl_Html::make_it_list( $this->error);
    }

    function print_error_debug(){
        foreach($this->error as $code => $message){
            echo "<br />";
            echo "<p class='error_code'>Error $code</p>";
            echo "<p class='error_message'> $message</p>";
            if (!empty ($this->error_data[$code]))
                echo "<p class='error_data'> $this->error_data[$code]</p>";
        }
    }

    function print_friendly_error(){

        krsort($this->errors);
        $full_message = '';
        foreach ( (array)$this->errors as $code => $message){
            $full_message .= $message.". ";
        }
        echo "<p class='error_message'>$full_message</p>";
    }

    function get_friendly_error() {
        krsort($this->errors);
        $full_message = '';
        foreach ( (array)$this->errors as $code => $message){
            $full_message .= $message.". ";
        }
        return "<p class='error_message'>$full_message</p>";
    }
}
?>
